package com.bawei.module_home.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ItemAreaBinding
import com.bawei.module_home.model.AreaModelItem


class AreaAdapter(): RecyclerView.Adapter<AreaAdapter.AreaViewHolder>() {
    val list=mutableListOf<AreaModelItem>()

    interface OnItemClickListener{
        fun clickListener(item: AreaModelItem, position: Int)
    }
    var itemClickLister: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener){
        itemClickLister=listener
    }
    // 定义点击回调接口
    interface OnItemLongClickListener {
        fun LongClickListener(item: AreaModelItem, position: Int)
    }
    var itemLongClickListener: OnItemLongClickListener? = null
    // 设置点击监听器
    fun setOnItemLongClickListener(listener: OnItemLongClickListener) {
        this.itemLongClickListener = listener
    }


    fun setData(data:List<AreaModelItem>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AreaViewHolder {
        val binding= ItemAreaBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )

        return AreaViewHolder(binding)
    }

    override fun getItemCount(): Int {
        Log.d("GZY", "getItemCount: "+list.size)
       return list.size
    }

    override fun onBindViewHolder(holder: AreaViewHolder, position: Int) {
        val binding=DataBindingUtil.getBinding<ItemAreaBinding>(holder.itemView) ?:return
        binding.areaTitle.text=list[position].spaceNumber
        binding.areaContext.text="所属区域:${list[position].areaName}"
        binding.areaType.text="车位类型:${list[position].spaceType}"
        binding.areaStatus.text=list[position].spaceStatus
        if(list[position].spaceStatus=="空闲"){
            binding.areaUser.visibility= View.GONE
            binding.areaQuestion.visibility= View.GONE
            binding.areaLicensePlate.visibility= View.GONE
            binding.areaStatus.background=ContextCompat.getDrawable(binding.root.context,R.drawable.area_status_green)
        }else if(list[position].spaceStatus=="维修中"){
            binding.areaUser.visibility= View.GONE
            binding.areaQuestion.visibility= View.VISIBLE
            binding.areaLicensePlate.visibility= View.GONE
            binding.areaQuestion.text="维护原因:${list[position].remarks}"
            binding.areaStatus.background=ContextCompat.getDrawable(binding.root.context,R.drawable.area_status_yellow)
        }else{
            binding.areaUser.visibility= View.VISIBLE
            binding.areaQuestion.visibility= View.GONE
            binding.areaLicensePlate.visibility= View.VISIBLE
            binding.areaUser.text="使用人:${list[position].userName}"
            binding.areaLicensePlate.text="车牌号:${list[position].licensePlate}"
            binding.areaStatus.background=ContextCompat.getDrawable(binding.root.context,R.drawable.area_status_blue)
        }

        holder.itemView.setOnLongClickListener{
            itemLongClickListener?.LongClickListener(list[position],position)
            true
        }

        holder.itemView.setOnClickListener{
            itemClickLister?.clickListener(list[position],position)
        }


    }

    class AreaViewHolder(item: ItemAreaBinding): RecyclerView.ViewHolder(item.root)
}

